//define(function(require,exports){

  angular.module('mobileApp')
      .controller('RegCtrl',["$scope","$window","$interval","$http","$location","$routeParams", function ($scope,$window,$interval,$http,$location,$routeParams) {
          //shopId和场景
          $scope.shopId = $window.localStorage.shopId ? $window.localStorage.shopId :'0';
          $scope.scene = $window.localStorage.scene ? $window.localStorage.scene :'0';

          //返回登录
          $scope.back = '<a href="#/login"><i class="icon-font" title="后退">&#xe61f;</i></a>';
          //表单
          $scope.user = {
              phone:"",
              randomCode:"",
              sms:"",
              password:"",
              repeatPassword:""
          };

          //获取短信验证码
          $scope.js_opt ="获取短信验证码";
          $scope.hasMobile = function(){
              if(!$scope.js_form.account.$error.pattern){
                 $scope.optValue = true;
              };
          }
          $scope.getSms = function(){
              var endTime = 30;
              $scope.optValue = false;
              $http({
                  method:"post",
                  url:hpfbase.url+"/sms/send-msg-no-pwd-login.jhtm",
                  headers: {
                     'Content-Type': "application/x-www-form-urlencoded;charset=utf-8"
                  },
                  data:'phone='+$scope.user.phone
              })
                  .success(function(ret){
                      if(ret.succeed){
                          var countdown = $interval(function(){
                              endTime--;
                              $scope.js_opt =endTime+"秒后重新获取";
                              if(endTime<=0){
                                  $scope.optValue = true;
                                  $interval.cancel(countdown);
                                  $scope.js_opt ="重新获取验证码";
                              }
                          },1000);
                      }else{
                          $scope.optValue = true;
                          $scope.js_form.sms.$sendError = ret.message;
                      }
                  });
          };

          //验证两次密码输入
          $scope.checkPassword = function(){
              $scope.js_form.repeatPassword.$error.repeat = $scope.user.password !== $scope.user.repeatPassword;
          }

          //提交
          var req= null;
          $scope.regForm = function(){
              $window.location.href= "shop.html#/category";
              if(req==null){
                  req = function(){
                      $http.post(hpfbase.url+"/u/user/register.jhtm",
                          'phone='+$scope.user.phone+
                          '&randomCode='+$scope.user.sms+
                          '&newPwd='+$scope.user.password+
                          '&shopId='+$scope.shopId
                          +'&recomId='+$scope.user.recomId,
                          {headers: {
                              'Content-Type': "application/x-www-form-urlencoded;charset=utf-8"
                          }})
                          .success(function(ret){
                              if(ret.successed){
                                  req= null;
                                  //没有shopID,没有查看店铺
                                  if($scope.shopId==0){
                                      $window.location.href="mine.html";
                                  }else{
                                      //如果有shopId.扫码，预订
                                      if($scope.scene=="bookNodish"){
                                          //扫码进来的

                                      }else if($scope.scene=="bookIndish"){
                                          //预订不点餐

                                      }else if($scope.scene=="bookIndish") {
                                          //预订点餐

                                      }else{
                                          //店铺进来
                                          $window.location.href= "shop.html#/category";
                                      }
                                  }
                              }else{
                                  req= null;
                                  $scope.hasError = "操作失败，验证码不正确！";
                                  $scope.step=0;
                              }
                          })
                          .error(function(){
                              $scope.step=0;
                          });
                  };
                  req();
              }
          };
      }]);
//});
